package com.statistics.service.supplementary;


import com.statistics.config.PressForMoneyService;
import com.statistics.config.YhSupplementService;
import com.statistics.entity.PressForMoneyDto;
import com.statistics.entity.SupplementDto;
import com.statistics.entity.YhSupplementDto;
import com.statistics.service.supplementary.thread.SupplementaryCallable;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;
import java.util.stream.Collectors;

/**
 * 补充记录
 * @author hewei
 * @date 2022/4/25 10:17
 * @Description
 */
@Service
@Slf4j
public class SupplementaryService {

    @Autowired
    private YhSupplementService yhSupplementService;

    @Autowired
    private PressForMoneyService pressForMoneyService;

    public Map<String, List<SupplementDto>> supplementary(MultipartFile yhFile, MultipartFile importFile) throws ExecutionException, InterruptedException {
        final List<YhSupplementDto> yhDtos = yhSupplementService.analysisYh(yhFile);
        final List<PressForMoneyDto> pressForMoneyDtos = pressForMoneyService.analysisYh(importFile);

        // 存储有数据但是缺失一些的
        Map<String,List<PressForMoneyDto>> availableData = new ConcurrentHashMap<>();
        // 存储有数据但是缺失一些的
        Map<String,YhSupplementDto> yhData = new ConcurrentHashMap<>();
        /**
         * 第一步 筛选关联数据  将身份证一样的集合存储起来
         */
        yhDtos.forEach(yhDto -> {
            if (availableData.containsKey(yhDto.getIdCard())){
                return;
            }
            List<PressForMoneyDto> collect = pressForMoneyDtos.stream().filter
                    (pfm -> pfm.getIdCard().equals(yhDto.getIdCard()) && StringUtils.isNotBlank(pfm.getResultDate())).collect(Collectors.toList());
            availableData.put(yhDto.getIdCard(),collect);
            yhData.put(yhDto.getIdCard(),yhDto);
        });
        yhDtos.clear();
        pressForMoneyDtos.clear();
        Callable<Map<String, List<SupplementDto>>> supplementaryCallable =  new SupplementaryCallable(availableData,yhData);
        /**
         * 自定义线程池来处理检查请求
         */
        ExecutorService executorService = Executors.newFixedThreadPool(4);
        // 创建多个有返回值的线程
        final Future<Map<String, List<SupplementDto>>> submit = executorService.submit(supplementaryCallable);
        final Future<Map<String, List<SupplementDto>>> submit1 = executorService.submit(supplementaryCallable);
        final Future<Map<String, List<SupplementDto>>> submit2 = executorService.submit(supplementaryCallable);
        final Future<Map<String, List<SupplementDto>>> submit3 = executorService.submit(supplementaryCallable);
        final Map<String, List<SupplementDto>> stringListMap = submit.get();
        final Map<String, List<SupplementDto>> stringListMap1 = submit1.get();
        final Map<String, List<SupplementDto>> stringListMap2 = submit2.get();
        final Map<String, List<SupplementDto>> stringListMap3 = submit3.get();
        Map<String, List<SupplementDto>> listMap = new HashMap<>();
        listMap.putAll(stringListMap);
        listMap.putAll(stringListMap1);
        listMap.putAll(stringListMap2);
        listMap.putAll(stringListMap3);
        executorService.shutdown();
        return listMap;
    }

}
